﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling.Diagrams;
using System.Drawing;
using Microsoft.VisualStudio.Modeling;

namespace UofCASE.AgileProductLinerDSL {

    public partial class TestShape {

        protected override void InitializeInstanceResources() {
            base.InitializeInstanceResources();

            AssociateValueWith(this.Store, Test.LastTestRunDomainPropertyId);
            AssociateValueWith(this.Store, Test.IsMappedDomainPropertyId);
            //after the resources are initialized, register for the collapsing change event for all TestPortShapes
            //this is connected to
            Test test = this.ModelElement as Test;
            if (test != null) {
                foreach (FeaturePort fPort in test.FeaturePorts) {
                    TestPortShape tPortShape = Diagram.FindShape(fPort.TestPort) as TestPortShape;
                    if (tPortShape != null) {
                        tPortShape.CollapsingChanged += OnCollapsedChangedTestShape;
                    }
                }
            }

            UpdateVisibility();
            UpdateBackgroundColor();
        }

        public override void OnDoubleClick(DiagramPointEventArgs e) {
            base.OnDoubleClick(e);

            Test test = this.ModelElement as Test;
            if (test == null) return;
            if (!test.IsMapped) return;
            DTEHelper.OpenAcceptanceTest(test.RelativePath, test.ProjectUniqueName);

        }

        protected override void OnAssociatedPropertyChanged(PropertyChangedEventArgs e) {
            UpdateBackgroundColor();
            base.OnAssociatedPropertyChanged(e);
        }
  
    }

    public partial class TestShapeBase {

        private Boolean GetIsVisibleDPValue() {
            Test test = this.ModelElement as Test;
            if (test.FeaturePorts.Count == 0) return true;
            foreach (FeaturePort fPort in test.FeaturePorts) {
                TestPortShape tPortShape = Diagram.FindShape(fPort.TestPort) as TestPortShape;
                if (tPortShape != null) {
                    if (!tPortShape.IsCollapsed) {
                        return true;
                    }
                }
            }
            return false;
        }
    }
}
